﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using QuanLyBanHang.DAL;
using QuanLyBanHang.OBJECT;

namespace QuanLyBanHang
{
    public partial class frmNhanVien : Form
    {
        public frmNhanVien()
        {
            InitializeComponent();
            dulieu = null;
        }

        DataTable dulieu;

        public frmNhanVien(DataTable dt)
        {
            InitializeComponent();
            dulieu = dt;
        }

        public void frmNhanVien_Load(object sender, EventArgs e)
        {
            if (dulieu == null)
            {
                dgvNV.DataSource = DalNhanVien.Load();
            }
            else
            {
                dgvNV.DataSource = dulieu;
            }
        }

        private void dgvNV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 8)
            {
                if (e.Value != null)
                {
                    e.Value = new string('*', e.Value.ToString().Length);
                }
            }
        }

        private void thêmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmEditNhanVien.Action = 1;
            frmEditNhanVien cn = new frmEditNhanVien(dgvNV, this);
            cn.ShowDialog();
        }

        private void sửaHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmEditNhanVien.Action = 2;
            frmEditNhanVien cn = new frmEditNhanVien(dgvNV, this);
            cn.ShowDialog();
        }

        private void hủyThaoTácToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("Bạn có chắc muốn xóa nhân viên " + dgvNV.CurrentRow.Cells[1].Value.ToString().ToUpper() + "?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    string MaNV = dgvNV.CurrentRow.Cells[0].Value.ToString();
                    string TaiKhoan = dgvNV.CurrentRow.Cells[7].Value.ToString();
                    int kq = DalNhanVien.Delete(MaNV);
                    int kq1 = DalUsers.Delete(TaiKhoan);
                    if (kq > 0 && kq1 > 0)
                        MessageBox.Show("Nhân viên: " + dgvNV.CurrentRow.Cells[1].Value.ToString().ToUpper() + " đã bị xóa khỏi CSDL", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        MessageBox.Show("Nhân viên: " + dgvNV.CurrentRow.Cells[1].Value.ToString().ToUpper() + " chưa được xóa khỏi CSDL", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            dgvNV.ClearSelection();
            frmNhanVien_Load(sender, e);
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return base.ProcessDialogKey(keyData);
            }
            else
                return false;
        }

    }
}
